/*************************************************************************
	> File Name: thread.hpp
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/10/03 15:13
 ************************************************************************/

#pragma once

#include <iostream>
#include <pthread.h>
#include <functional>

typedef void*(*func_t)(void*);

class ThreadData
{
public:
    int _sock;
    std::string _ip;
    uint64_t _port;
};

class Thread
{
public:
    Thread (int sock, std::string ip, uint64_t port, func_t func)
    {
        _tdata._sock = sock;
        _tdata._ip = ip;
        _tdata._port = port;
        _func = func;
    }

    void run()
    {
        pthread_create(&_tid, nullptr, _func, &_tdata);
    }

    void join()
    {
        pthread_join(_tid, nullptr);
    }

private:
    ThreadData _tdata;
    func_t _func;
    pthread_t _tid;
};
